var busy = false;
var boyUrl="http://g4cdn.shfyuan.com/ws/Mobile/Webroot/gf/images/newYear/p2/bo1.png",girlUrl = "http://g4cdn.shfyuan.com/ws/Mobile/Webroot/gf/images/newYear/p2/gri1.png",
boySelectUrl="http://g4cdn.shfyuan.com/ws/Mobile/Webroot/gf/images/newYear/p2/bo2.png",girlSelectUrl = "http://g4cdn.shfyuan.com/ws/Mobile/Webroot/gf/images/newYear/p2/gri2.png";
var firstvote = $("#voteFirst"),secondvote = $("#voteSecond"),timeCount = $("#timeCount"),girlEml = $("#gri"),boyEml = $("#bo");
var waitingHB = $(".waitingHB").eq(0),getHB = $(".getHB").eq(0),mymuis = document.getElementById("musicfx");
var isQiang = false;
function changeSrc(num)
{
	if(selectVote==num){
		return;
	}
	if(busy){
		warn('正在提交，请稍候再试...');
		return;
	}
	busy = true;
	warn("投票中...");
	mymuis.play();
	mymuis.loop=false;
	mymuis.addEventListener('ended',function(){
	},false);
	var url = "index.php?g=mobile&m=newYear&a=sendVote";
	var data = {
			"vote":num
	}
	$.post(url,data,function(result){
		if(result.error==0)
		{
			selectVote = num;
			switch(num){
				case 1:
					girlEml.attr("src",girlSelectUrl);
					boyEml.attr("src",boyUrl);
					break;
				case 2:
					girlEml.attr("src",girlUrl);
					boyEml.attr("src",boySelectUrl);
					break;
			}
			firstvote.html(result.vote.first);
			secondvote.html(result.vote.second);
		}
		warn(result.content);
		busy = false;
	},"json")
}
function hiddenImg()
{
	document.getElementById("qhb_page").style.display="none";
}
//
$(function(){
	countDown();
	OminuteCount();
})

function countDown(){
	itemScale = setInterval(function(){
		nowTime++;
		var active = getHongbao();
		if(active=="noActivity"){
			var eml = "活动已结束";
			timeCount.html(eml);
			waitingHB.show();
			getHB.hide();
		}else if(active.hongbao=="get"){
			waitingHB.hide();
			getHB.show();
		}else{
			waitingHB.show();
			getHB.hide();
			var eml  = ShowCountDown(active);
			timeCount.html(eml);
		}
	},1000);
}
function OminuteCount(){
	itemZbActivity = setInterval(function(){
		var ZBurl = "index.php?g=mobile&m=newYear&a=refreshActivity";
		$.post(ZBurl,{},function(result){
			if(result.error==0)
			{
				zbActivity = eval(result.data);
			}
		},"json");
	},59430);
}

function getHongbao(){
	for(var i=0;i<7;i++){
		if(nowTime<=zbActivity[i].begin_time){
			zbActivity[i].hongbao = "waiting";
			isQiang = false;
			return zbActivity[i];
		}else{
			if(nowTime<=zbActivity[i].end_time){//
				zbActivity[i].hongbao = "get";
				isQiang = true;
				return zbActivity[i];
			}else{
				continue;
			}
		}
	}
	return "noActivity";
}
function ShowCountDown(active) 
{ 
	if(active.hongbao=="get"){
		var leftsecond = active.end_time - nowTime; 
		var hour=Math.floor((leftsecond)/3600); 
		var minute=Math.floor((leftsecond-hour*3600)/60); 
		var second=Math.floor(leftsecond-hour*3600-minute*60); 
		return "距离红包结束还有"+hour+"时"+minute+"分"+second+"秒"; 
	}else{
		var leftsecond = active.begin_time - nowTime; 
		var hour=Math.floor((leftsecond)/3600); 
		var minute=Math.floor((leftsecond-hour*3600)/60); 
		var second=Math.floor(leftsecond-hour*3600-minute*60); 
		return "距离抢红包还有  "+hour+"时"+minute+"分"+second+"秒"; 
	}
} 

function qiangHB(){
	if(busy){
		warn('正在提交，请稍候再试...');
		return;
	}
	if(!selectVote){
		warn("投票后方可抢红包");
		return;
	}
	if(!isQiang){
		warn("未到抢红包的精彩时刻");
		return;
	}
	var HB = getHongbao();
	if(HB.zbhb_id == 9999||HB.zbhb_id == "9999"){
		window.location.href="index.php?g=mobile&m=car&a=picao";
		return ;
	}
	if(HB.zbhb_id==0){
		warn('很遗憾没有抢到红包');
		return ;
	}
//	if(HB=="noActivity"){
//		window.location.href="index.php?g=mobile&m=car&a=index";
//	}else{
	$("#qhb_page").show();
//	}
}
function getZBHB(){
	if(busy){
		warn('正在提交，请稍候再试...');
		return;
	}
	busy = true;
	var HB = getHongbao();
	if(HB=="noActivity"||HB.hongbao=="waiting"||HB.zbhb_id==0){
		hiddenImg();
		warn('本轮抢红包活动未开始');
		busy = false;
		return ;
	}
	if(HB.zbhb_id == 9999||HB.zbhb_id == "9999"){
		window.location.href="index.php?g=mobile&m=car&a=picao";
		return ;
	}
	warn("领取红包中...");
	var data = {
			zbhb_id:HB.zbhb_id,
			wxid:wxid
	};
	var url="index.php?g=mobile&m=newYear&a=getZBHongbao";
	$.post(url,data,function(result){
		if(result.error==1){
			warn(result.content);
		}else{
			zbHongbao(result.result);
		}
		busy = false;
	},"json");
}
function zbHongbao(data){
		//跳转到抽红包页面
	     BeaconShakehbJsBridge.invoke('jumpHongbao',{
		     lottery_id:data.lottery_id,
		     noncestr:data.noncestr,
		     openid:data.openid,
		     sign:data.sign
	     });
}
//不打断alert
function warn(msg,opt,left,top){
	if(opt){
	 	var obj = $("#"+opt);
	}
 	new Toast({context:$('body'),message:msg},obj,left,top).show();
}
var Toast = function(config,obj,left,top){
	this.context = config.context==null?$('body'):config.context;//上下文
	this.message = config.message;//显示内容
	this.time = config.time==null?5000:config.time;//持续时间
	this.left = config.left;//距容器左边的距离
	this.top = ($(document).height()/4)*3;//距容器上方的距离
	if(obj){ 
	this.left = obj.offset().left + left;
	this.top = obj.offset().top + top;
	}
	this.init();
}
var msgEntity;
Toast.prototype = {
	//初始化显示的位置内容等
	init : function(){
		$("#toastMessage").remove();
		//设置消息体
		var msgDIV = new Array();
		msgDIV.push('<div id="toastMessage">');
		msgDIV.push('<span>'+this.message+'</span>');
		msgDIV.push('</div>');
		msgEntity = $(msgDIV.join('')).appendTo(this.context);
		//设置消息样式
		var left = this.left == null ? this.context.width()/2-17-msgEntity.find('span').width()/2 : this.left;
		var top = this.top == null ? '20px' : this.top;
		msgEntity.css({position:'absolute',top:top,'z-index':'9999',left:left,'background-color':'rgb(95, 99, 109)',color:'white','font-size':'14px',padding:'12px',margin:'5px','border-radius':'4px','-moz-border-radius':'4px','-webkit-border-radius':'4px','font-family':'微软雅黑'});
		//msgEntity.addClass(".toast");
		msgEntity.hide();
 	},
	//显示动画
	show :function(){
		msgEntity.fadeIn(this.time/2);
		msgEntity.fadeOut(this.time/2);
	}
}
